package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;
@RestController
@Component
@Slf4j
public class CommonExceptionResolver {
    //处理异常指定处理哪一类异常:处理所有的Exception
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handlerException(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        String message = e.getMessage();
        //Duplicate entry 'wanwu' for key 'idx_username'
        String[] split = message.split(",");
        return R.error("账号"+split[1]+"已存在");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
/*
    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e) {
    e.printStackTrace();
    return R.error(e.getMessage());
        }
 */
}
